package com.bigdata.booklibraryseatapi.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigdata.booklibraryseatapi.common.exception.BusinessException;
import com.bigdata.booklibraryseatapi.common.exception.ResponseCode;
import com.bigdata.booklibraryseatapi.common.pojo.PageResult;
import com.bigdata.booklibraryseatapi.common.utils.JwtUtil;
import com.bigdata.booklibraryseatapi.entity.UserApp;
import com.bigdata.booklibraryseatapi.mapper.UserAppMapper;
import com.bigdata.booklibraryseatapi.service.IUserAppService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.booklibraryseatapi.vo.req.EditUserReqVO;
import com.bigdata.booklibraryseatapi.vo.req.LoginReqVO;
import com.bigdata.booklibraryseatapi.vo.req.PageUserReqVO;
import com.bigdata.booklibraryseatapi.vo.req.RegisterReqVO;
import com.bigdata.booklibraryseatapi.vo.resp.LoginRespVO;
import com.bigdata.booklibraryseatapi.vo.resp.PageUserRespVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 小程序用户表 服务实现类
 * </p>
 *
 * @author book
 * @since 2023-12-10
 */
@Service
public class UserAppServiceImpl extends ServiceImpl<UserAppMapper, UserApp> implements IUserAppService {

    @Resource
    private JwtUtil jwtUtil;

    @Resource
    private UserAppMapper userAppMapper;

    @Override
    public LoginRespVO login(LoginReqVO vo) {
        UserApp user = query().select("password", "id").last("limit 1").eq("username", vo.getUsername()).one();
        if (user == null) {
            throw new BusinessException(ResponseCode.ACCOUNT_NOT_FOUND.getCode(), ResponseCode.ACCOUNT_NOT_FOUND.getMessage());
        }
        if (!SecureUtil.md5(vo.getPassword()).equals(user.getPassword())) {
            throw new BusinessException(ResponseCode.ACCOUNT_ERROR.getCode(), ResponseCode.ACCOUNT_ERROR.getMessage());
        }
        LoginRespVO result = new LoginRespVO();
        result.setId(user.getId());
        result.setToken(jwtUtil.createToken(String.valueOf(user.getId()), null));
        result.setUsername(vo.getUsername());
        return result;
    }

    @Override
    public LoginRespVO adminLogin(LoginReqVO vo) {
        if (!vo.getUsername().equals("admin")) {
            throw new BusinessException(ResponseCode.NO_ADMIN_USER_EXISTS.getCode(), ResponseCode.NO_ADMIN_USER_EXISTS.getMessage());
        }
        return login(vo);
    }

    @Override
    public void editUser(EditUserReqVO vo) {
        UserApp user = userAppMapper.selectById(vo.getId());
        if (vo.getIsPassword()) user.setPassword(SecureUtil.md5(vo.getPassword()));
        if (vo.getIsStatus()) user.setStatus(vo.getStatus());
        int result = userAppMapper.updateById(user);
        if (result != 1) {
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public void register(RegisterReqVO vo) {
        UserApp user = query().select("id").last("limit 1").eq("username", vo.getUsername()).one();
        if (user != null) {
            throw new BusinessException(ResponseCode.ACCOUNT_ALREADY_EXISTS.getCode(), ResponseCode.ACCOUNT_ALREADY_EXISTS.getMessage());
        }
        UserApp insertUser = new UserApp();
        BeanUtils.copyProperties(vo, insertUser);
        insertUser.setPassword(SecureUtil.md5(vo.getPassword()));
        insertUser.setStatus(1);
        int result = userAppMapper.insert(insertUser);
        if (result != 1) {
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public PageResult<PageUserRespVO> pageUsers(PageUserReqVO vo) {
        IPage<PageUserRespVO> result = userAppMapper.selectPageUser(new Page<>(vo.getCurrent(), vo.getSize()), vo);
        return PageResult.getPage(result.getRecords(), result.getTotal());
    }
}
